(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.reportreqController', reportreqController);
	reportreqController.$inject = [ '$scope', '$q', '$uibModalInstance', '$filter', '$interval', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'reportreqService', 'commonService' ];
	function reportreqController($scope, $q, $uibModalInstance, $filter, $interval, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, reportreqService, commonService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("reportreqLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.currReportreq = {};
		var hasCancelTimer = false;
		
		ctrl.init = function() {
			ctrl.currReportreq.currReportreqId = ctrl.dialogsModule.params.requestId;
			
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			reportreqService.initListSearchOptions()//1. search option
			.then(function(result) {
				return $q(function(resolved, rejected) {
					ctrl.reportreqStatusOptions = result.reportreqStatusOptions || [];
					resolved();
				});
			})
			.then(function() {// 2. get list
				if (ctrl.dialogsModule.params.reportCode) {
					ctrl.searchCriteria.reportCode = $filter('encode')(ctrl.dialogsModule.params.reportCode);// get params from parent page
				} else {
					delete ctrl.searchCriteria.reportCode;
				}
				ctrl.search();
			});
			
		};
		
		// if has processing will reload 10s for get generated item
		ctrl.timer = $interval(function() {
			if (!hasProcessingAsynReport()) {
				$interval.cancel(ctrl.timer);
				hasCancelTimer = true;
			} else {
				ctrl.search();
			}
		}, 10000);
		
		ctrl.viewDocument = function(rowObj) {
			rowObj.srcTxnRef  = $filter('encode')(rowObj.requestId);
			rowObj.isTemporary = "N";
			commonService.openDocument(rowObj, 'do/cda/reportreq/download');
		};
		
		ctrl.downloadDocument = function(rowObj) {
			rowObj.srcTxnRef  = $filter('encode')(rowObj.requestId);
			rowObj.isTemporary = "N";
			commonService.download(rowObj, 'do/cda/reportreq/download');
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		function hasProcessingAsynReport() {
			if (ctrl.searchCriteria && ctrl.searchCriteria.list) {
				for (var i = 0; i < ctrl.searchCriteria.list.length; i++) {
					var item = ctrl.searchCriteria.list[i];
					if (item && (item.reportStatus == 'I' || item.reportStatus == 'C')) 
						return true;
				}
			}
			return false;
		}
		
//		ctrl.ok = function() {
//			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
//				$uibModalInstance.close();
//			}
//		};
		
		ctrl.close = function() {
			// if no calcel, this place must cancel
			if (!hasCancelTimer)
				$interval.cancel(ctrl.timer);
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
	}
})(angular);
